package com.jht.bletool2java.characteristic.userdata;

import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;

import androidx.annotation.Keep;

import com.jht.bletool2java.characteristic.TranslateData;

import java.nio.charset.StandardCharsets;

import top.codestudy.annotation_uuid.MyUUID;

@Keep
@MyUUID(uuid = "00002ab5-0000-1000-8000-00805f9b34fb")
public class LocationName implements TranslateData {
    private static final String TAG = "LocationName";
    private String locationName = "";
    private byte[] value;
    private boolean isInvalidData = false;
    public LocationName() {

    }

    public LocationName(byte[] value) {
        this.value = value;
        locationName += new String(value, StandardCharsets.UTF_8);
    }

    public LocationName(BluetoothGattCharacteristic characteristic) {
        value = characteristic.getValue();
        locationName += new String(value, StandardCharsets.UTF_8);
    }

    @Override
    public String convert2String() {
        return "LocationName: " + locationName + "\n";
    }

    @Override
    public String merge(TranslateData translateData) {
        return null;
    }

    @Override
    public boolean hasMoreData() {
        return false;
    }

    @Override
    public byte[] getData() {
        return value;
    }

    @Override
    public boolean isInvalidData() {
        return isInvalidData;
    }

    @Override
    public void parseData(BluetoothGattCharacteristic characteristic, byte[] value) {
        isInvalidData = value == null;
        if (isInvalidData) {
            //数据非法
            this.value = null;
            locationName = "";
            Log.w(TAG, "parseData: value == null");
        }else{
            this.value = value;
            locationName = new String(value, StandardCharsets.UTF_8);
        }
    }
}
